﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ValidaterPicture;
using Model;
using Bll;
using System.IO;

namespace WebPages.Pages
{
    public partial class User_Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
      
        protected void ChectButton_Click(object sender, EventArgs e)
        {
            string SID = SIDTextBox.Text;
            string account = AccountTextBox.Text;
            string pwd = PwdTextBox.Text;
            string pwd_again = PwdTextBox_Check.Text;
            string name = NameTextBox.Text;
            string email = EmailTextBox.Text;
            string addr = null;
           // if(EmailTextBox.Text)
            if (AcpUpload.HasFile)
            {
                //图片
                string fileName = AcpUpload.FileName;
                addr = "..//Identify//" + SID + Path.GetExtension(fileName);
                //保存图片
                try
                {
                    AcpUpload.SaveAs(Server.MapPath(addr));
                }catch(Exception ex)
                {
                    Response.Write("<script>alert('图片上传失败，请重试。')</script>");
                    return;
                }
                
               // addr = "..//Identify" + fileName;
                User_info ul = new User_info();
                ul.SID = SID;
                ul.account = account;
                ul.pwd = pwd;
                ul.name = name;
                ul.acceptpicture = addr;
                ul.email = email;
                BllLayer bll = new BllLayer();
                int flag = bll.register(ul);
                if (flag == 1)
                {
                    Response.Write("<script>alert('注册成功,请耐心等待验证,验证完成后会通过您填写的邮箱通知您。')</script>");
                }
                else
                {
                    Response.Write(string.Format("<script>alert('出错，错误代码{0}');", flag));
                }
            }
            else
            {
                Response.Write("<script>alert('未知错误，请重试！')</script>");
            }
           



        }

       protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        {
            string account = Login1.UserName;
            string pwd = Login1.Password;
            BllLayer bll = new BllLayer();
            int flag = bll.login(account, pwd);
            if (flag == 1)
            {
                Login1.DestinationPageUrl = "HomePage.aspx";
                Session.Add("UID", bll.GetUserInfo(account).SID);
                e.Authenticated = true;
            }
            else if (flag == 0)
            {
                Login1.FailureText = "密码错误";
                e.Authenticated = false;
            }else if(flag == -1)
            {
                Login1.FailureText = "你的账号或学号不存在";
                e.Authenticated = false;
            }
            else if (flag == -3)
            {
                Login1.FailureText = "你的账号还在认证，请耐心等待。";
                e.Authenticated = false;
            }
            else if (flag == -2)
            {
                Login1.FailureText = "异常，请联系管理员 QQ：1584643004";
                e.Authenticated = false;
            }
        }

    }
}